﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class CapNhatDaiLy : XtraUserControl
    {
        public CapNhatDaiLy()
        {
            InitializeComponent();
            LoadDanhSachLoaiDaiLy();
            LoadDanhSachQuan();
            LoadDanhSachTenDaiLy();
        }
        public CapNhatDaiLy(string MaDaiLy, string TenDaiLy, string TenLoaiDaiLy, string TenQuan, string DiaChi, string DienThoai, string Email, string NgayTiepNhan)
        {
            InitializeComponent();
           
            LoadCapNhatDaiLy(MaDaiLy, TenDaiLy, TenLoaiDaiLy, TenQuan, DiaChi, DienThoai, Email, NgayTiepNhan);
            LoadDanhSachLoaiDaiLy();
            LoadDanhSachQuan();
            LoadDanhSachTenDaiLy();
            lookupTenDaiLy.EditValue = MaDaiLy; 
            
        }
        private void LoadDanhSachTenDaiLy()
        {
            List<DaiLyDTO> list = new List<DaiLyDTO>();
            list = DaiLyDAO.LoadDanhSachDaiLy2();
            lookupTenDaiLy.Properties.DataSource = list;
            lookupTenDaiLy.Properties.ValueMember = "MaDaiLy";
            lookupTenDaiLy.Properties.DisplayMember = "TenDaiLy";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenDaiLy");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupTenDaiLy.Properties.Columns.Add(col);
        }
        private void LoadDanhSachQuan()
        {
            List<QuanDTO> list = new List<QuanDTO>();
            list = QuanDAO.LoadDanhSachQuan();
            lookupQuan.Properties.DataSource = list;
            lookupQuan.Properties.ValueMember = "MaQuan";
            lookupQuan.Properties.DisplayMember = "TenQuan";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenQuan");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupQuan.Properties.Columns.Add(col);
        }
        private void LoadDanhSachLoaiDaiLy()
        {
            List<LoaiDaiLyDTO> list = new List<LoaiDaiLyDTO>();
            list = LoaiDaiLyDAO.LoadDanhSachLoaiDaiLy();
            lookupLoaiDaiLy.Properties.DataSource = list;
            lookupLoaiDaiLy.Properties.ValueMember = "MaLoaiDaiLy";
            lookupLoaiDaiLy.Properties.DisplayMember = "TenLoaiDaiLy";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenLoaiDaiLy");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupLoaiDaiLy.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private void btnConHinh_Click(object sender, EventArgs e)
        {
            OpenFileDialog open;
            open = new OpenFileDialog();
            open.InitialDirectory = "D:";
            DialogResult result = open.ShowDialog();
            if (result == DialogResult.OK)
            {
                pictureEdit.Image = Image.FromStream(open.OpenFile());
            }
        }
        private void LoadCapNhatDaiLy(string MaDaiLy, string TenDaiLy, string TenLoaiDaiLy, string TenQuan, string DiaChi, string DienThoai, string Email, string NgayTiepNhan)
        {
            lookupLoaiDaiLy.ClosePopup();
            txtDiaChi.Text = DiaChi;
            txtEmail.Text = Email;
            txtDienThoai.Text = DienThoai;
            lookupQuan.Text = TenQuan;
           // lookupQuan.ClosePopup();
            lookupLoaiDaiLy.Properties.DisplayMember = TenLoaiDaiLy;
           // lookupLoaiDaiLy.ClosePopup();
        }
       private void btnCapNhatDaiLy_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable quiDinh = QuiDinhDAO.DanhSachQuiDinh();
                int quan = lookupQuan.EditValue.GetHashCode();
                int slDaiLyTrongQuan = DaiLyDAO.SoDaiLyToiDaTrongQuan(quan);
                int slDaiLyTheoQD = quiDinh.Rows[0][0].GetHashCode();

                DaiLyDTO dl = new DaiLyDTO();
                dl.MaDaiLy = lookupTenDaiLy.EditValue.ToString();
                dl.TenDaiLy = lookupTenDaiLy.Text;
                dl.MaLoaiDaiLy = Convert.ToInt32(lookupLoaiDaiLy.EditValue.ToString());
                dl.MaQuan = int.Parse(lookupQuan.EditValue.ToString());
                dl.DiaChi = txtDiaChi.Text;
                dl.DienThoai = txtDienThoai.Text;
                dl.Email = txtEmail.Text;
                if (KiemTraTextBox() == true)
                {
                    if (slDaiLyTheoQD <= slDaiLyTrongQuan)
                    {
                        MessageBox.Show("Số Đại Lý Trong Quận vượt mức qui định");
                        GiaoDienMacDinh();
                    }
                    else
                    {
                        if (DaiLyDAO.CapNhatDaiLy(dl) == true)
                        {
                            MessageBox.Show("Cập nhật thành công !");
                            // dataGridView.Rows.Add();
                            GiaoDienMacDinh();
                        }
                        else
                        {
                            MessageBox.Show("Có lổi khi tiếp nhận đại lý");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Không được để trống thông tin");
                }
            }
            catch (Exception)
            {

            }
        }

        private void GiaoDienMacDinh()
        {
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtDienThoai.Text = "";
           // lookupQuan.ItemIndex=-1;
           // lookupQuan.ClosePopup();
           // lookupLoaiDaiLy.ItemIndex = -1;
           // lookupLoaiDaiLy.ClosePopup();
        }
        private bool KiemTraTextBox()
        {
            if (txtDiaChi.Text == "" || txtEmail.Text == "" || txtDienThoai.Text == "")
                return false;
            return true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnXemDanhSachDaiLy_Click(object sender, EventArgs e)
        {
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            XemDanhSachDaiLy frm = new XemDanhSachDaiLy();
            tb.Controls.Add(frm, 0, 1);
        }

        private void CapNhatDaiLy_Load(object sender, EventArgs e)
        {
        }

        private void lookupTenDaiLy_EditValueChanged(object sender, EventArgs e)
        {
            string madaily = lookupTenDaiLy.EditValue.ToString();
            DataTable dl = DaiLyDAO.LayDanhSachDaiLyTheoMa(madaily);
            DataRow dr = dl.Rows[0];
            txtDiaChi.Text = dr["DiaChi"].ToString();
            txtDienThoai.Text = dr["DienThoai"].ToString();
            txtEmail.Text = dr["Email"].ToString();
            lookupQuan.EditValue = dr["Quan"].ToString();
            lookupLoaiDaiLy.EditValue = dr["MaLoaiDaiLy"].ToString();
        }
    }
}
